<?php
// +----------------------------------------------------------------------
// |商品分类
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午3:35
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class ProductCateController extends AdminCommonController{
    private $model;
    
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = D('Admin/ProductCate');
    }

    public function index(){
        $map = array();
        if(!$this->isSysAdmin){
            $map['uid'] = $this->manage_id;
        }
        $list = $this->model->getList($map,'sort asc,add_time desc');
        foreach($list['data'] as &$val){
            $val['shopTitle'] = D('Common/Shop')->detail($val['shop_id'],'title');
        }
        $this->assign($list);
        
        $this->display('index');
    }

    /**
     * 添加或编辑显示
     */
    public function edit(){
        $id = I('get.id');
        if($id){
            $detail = $this->model->detail($id);
            $this->assign($detail);
        }
        $shopList = D('Common/Shop')->getShopList($this->manage_id);
        $this->assign('shopList',$shopList);
        $this->display('edit');
    }

    /**
     * 添加或更新操作
     */
    public function update(){
        if(IS_POST){
            $id = I('post.id');
            $title = I('post.title');//银行名称
            $sort = I('post.sort',0,'intval');//排序
            $shop_id = I('post.shop_id');//店铺ID

            empty($shop_id) && $this->returnData('请选择所属分店!');

            if(empty($title)){
                $this->returnData('分类名称不能为空');
            }
            if(abslength($title) > 30){
                $this->returnData('分类名称不能超过30个字');
            }

            if($_SESSION['administrator']){
                $detail = D('Common/Shop')->detail($shop_id);
                $uid = $detail['uid'];
            }else{
                $uid = $this->manage_id;
            }

            $isExists = $this->model->checkCateTitleExists($shop_id,$title,$id);
            if($isExists){
                $this->returnData('此分类已经存在');
            }

            if(!preg_match('/^\d{0,5}$/',$sort)){
                $this->returnData('排序输入错误');
            }

            $data = array(
                'title'=>$title,
                'sort' => $sort,
                'shop_id'=>$shop_id
            );

            if($id){
                $msg = '修改';
            }else{
                $msg = '添加';
                $data['uid'] = $uid;
                $data['add_time'] = NOW_TIME;
            }

            $result = $this->model->update($data,$id);

            if($result){
                $this->clearCache($id);
                $this->model->clearCache($data['shop_id']);
                $this->addHistoryLog($msg.'商品分类成功！');
                $this->returnData($msg.'成功！',1);
            }else{
                $this->returnData($msg.'失败！');
            }
        }else{
            $this->returnData('提交方式错误！');
        }
    }

    /**
     * 排序
     */
    public function sort(){
        $id = I('post.id',0,'intval');
        $sort = I('post.sort',0,'intval');//最终数字

        empty($id) && $this->returnData('请选择操作项！');
        if(abslength($sort) > 5){
            $this->returnData('排序数字不能超过5位');
        }

        if($this->model->setSort($id,$sort)){
            $this->clearCache($id);
            $detail = $this->model->detail($id);
            $this->model->clearCache($detail['shop_id']);
            $this->addHistoryLog('商品分类排序成功！');
            $this->returnData('操作成功！',1);
        }else{
            $this->returnData('操作失败！');
        }
    }

    /**
     * 删除
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id');
            empty($id) && $this->returnData('请选择删除的银行！');
            
            $hasProduct = $this->model->hasProduct($id);
            if($hasProduct){
                $this->returnData('此分类已有商品，暂无法删除');
            }

            if($this->model->del($id)){
                $this->clearCache($id);
                $detail = $this->model->detail($id);
                $this->model->clearCache($detail['shop_id']);
                $this->addHistoryLog('删除商品分类成功！');
                $this->returnData('删除成功！',1);
            }else{
                $this->returnData('删除失败！');
            }
        }else{
            $this->returnData('提交方式错误！');
        }
    }


    private function clearCache($id){
        D('Common/ProductCate')->clearCache($this->manage_id,$id);
    }
}
?>